#include "stdafx.h"

#include "StickyTweetsXP.h"

#include "SettingsDialog.h"
#include "StickyTweetsAppXP.h"

HANDLE hDonateBitmap(NULL);

INT_PTR CALLBACK SettingsDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)SettingsDialog_Init(hDlg);

	case WM_COMMAND:
    if (HIWORD(wParam) == BN_CLICKED)
    {
		  switch (LOWORD(wParam))
      {
      case IDC_CMD_SETTINGS_FILTER_REMOVE:
        SettingsDialog_CmdFilterRemove_Clicked(hDlg);
        return (INT_PTR)TRUE;
        break;

      case IDC_CMD_DONATE:
        ShellExecute(NULL, NULL, L"https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=5259553", NULL, NULL, SW_SHOWNORMAL);
        return (INT_PTR)TRUE;
        break;

      case IDOK:
			  return (INT_PTR)SettingsDialog_CmdOk_Clicked(hDlg);

      case IDCANCEL:
        EndDialog(hDlg, StatusStOk);
        return (INT_PTR)TRUE;
		  }
    }
		break;

  case WM_DESTROY:
    DeleteObject(hDonateBitmap);
    return (INT_PTR)TRUE;
	}
	return (INT_PTR)FALSE;
}

BOOL SettingsDialog_Init(HWND hDlg)
{
  StickyTweetsAppXP& app = StickyTweetsAppXP::GetInstance();
  SetDlgItemText(hDlg, IDC_LBL_VERSION, app.GetVersion());

  Settings settings = app.GetSettings();

  TCHAR loadString[MaxLoadString];
  FLOAT fVal = static_cast<FLOAT>(settings.RefreshPeriod) / 60000.0f;
  if (fmod(fVal, 1.0f) > 0.0f)
    swprintf_s<MaxLoadString>(loadString, L"%0.2f", fVal);
  else
    swprintf_s<MaxLoadString>(loadString, L"%d", settings.RefreshPeriod / 60000);
  SetDlgItemText(hDlg, IDC_TXT_SETTINGS_REFRESH, loadString);

  swprintf_s<MaxLoadString>(loadString, L"%d", settings.HomeMax);
  SetDlgItemText(hDlg, IDC_TXT_SETTINGS_HOMEMAX, loadString);

  if (settings.NoBalloonsByDefault)
    CheckDlgButton(hDlg, IDC_CHK_SETTINGS_NOBALLOONS, BST_CHECKED);

  if (settings.NoImages)
    CheckDlgButton(hDlg, IDC_CHK_SETTINGS_NOIMAGES, BST_CHECKED);

  CAtlList<CString> filters;
  app.GetSearchFilterList(filters);
  POSITION e = filters.GetHeadPosition();
  while (e)
  {
    CString& filter = filters.GetNext(e);
    SendDlgItemMessage(hDlg, IDC_LB_SETTINGS_FILTER, LB_ADDSTRING, NULL, (LPARAM)filter.GetString());
  }

  SendDlgItemMessage(hDlg, IDC_TXT_SETTINGS_TRIMUSER, EM_SETCUEBANNER, 0, (LPARAM)L" username");
  SetDlgItemText(hDlg, IDC_TXT_SETTINGS_TRIMUSER, settings.TrimUser);
  SendDlgItemMessage(hDlg, IDC_TXT_SETTINGS_TRIMPASS, EM_SETCUEBANNER, 0, (LPARAM)L" password");
  SetDlgItemText(hDlg, IDC_TXT_SETTINGS_TRIMPASS, settings.TrimPass);

  swprintf_s<MaxLoadString>(loadString, L"%d", settings.TrimMin);
  SetDlgItemText(hDlg, IDC_TXT_SETTINGS_TRIMMIN, loadString);

  hDonateBitmap = LoadImage(hInst, MAKEINTRESOURCE(IDB_DONATE), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
  SendDlgItemMessage(hDlg, IDC_CMD_DONATE, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hDonateBitmap);

  return TRUE;
}

VOID SettingsDialog_CmdFilterRemove_Clicked(HWND hDlg)
{
  INT curSel = static_cast<INT>(SendDlgItemMessage(hDlg, IDC_LB_SETTINGS_FILTER, LB_GETCURSEL, 0, 0));
  if (curSel != LB_ERR)
    SendDlgItemMessage(hDlg, IDC_LB_SETTINGS_FILTER, LB_DELETESTRING, curSel, 0);
}

BOOL SettingsDialog_CmdOk_Clicked(HWND hDlg)
{
  INT statusCode = ERROR_SUCCESS;
  BOOL redraw = FALSE;

  Settings settings = StickyTweetsAppXP::GetInstance().GetSettings();

  TCHAR loadString[MaxLoadString];
  GetDlgItemText(hDlg, IDC_TXT_SETTINGS_REFRESH, loadString, MaxLoadString);
  FLOAT fVal;
  if (swscanf_s(loadString, L"%f", &fVal) != 1)
    statusCode = IDS_ERROR_SETTINGS_REFRESH;

  if (statusCode == ERROR_SUCCESS)
  {
    DWORD refreshPeriod = static_cast<DWORD>(floorf(fVal * 60000.0f));
    if (refreshPeriod >= 60000)
      settings.RefreshPeriod = refreshPeriod;
    else
      statusCode = IDS_ERROR_SETTINGS_REFRESH;
  }

  if (statusCode == ERROR_SUCCESS)
  {
    INT iVal;
    GetDlgItemText(hDlg, IDC_TXT_SETTINGS_HOMEMAX, loadString, MaxLoadString);
    if (swscanf_s(loadString, L"%d", &iVal) != 1 || iVal <= 0)
      statusCode = IDS_ERROR_SETTINGS_HOMEMAX;
    
    if (statusCode == ERROR_SUCCESS)
      settings.HomeMax = iVal;
  }

  settings.NoBalloonsByDefault = IsDlgButtonChecked(hDlg, IDC_CHK_SETTINGS_NOBALLOONS) == BST_CHECKED ? TRUE : FALSE;

  BOOL noImages = IsDlgButtonChecked(hDlg, IDC_CHK_SETTINGS_NOIMAGES) == BST_CHECKED ? TRUE : FALSE;
  if (noImages != settings.NoImages)
    redraw = TRUE;
  settings.NoImages = noImages;

  settings.SearchFilters.Empty();
  TCHAR filter[MaxTwitterStatus];
  INT filterCount = static_cast<INT>(SendDlgItemMessage(hDlg, IDC_LB_SETTINGS_FILTER, LB_GETCOUNT, 0, 0));
  for (INT i = 0; i < filterCount; i++)
  {
    SendDlgItemMessage(hDlg, IDC_LB_SETTINGS_FILTER, LB_GETTEXT, i, (LPARAM)filter);
    if (settings.SearchFilters.GetLength() > 0)
      settings.SearchFilters += L"\n";
    settings.SearchFilters += filter;
  }

  GetDlgItemText(hDlg, IDC_TXT_SETTINGS_TRIMUSER, loadString, MaxLoadString);
  settings.TrimUser = loadString;

  GetDlgItemText(hDlg, IDC_TXT_SETTINGS_TRIMPASS, loadString, MaxLoadString);
  settings.TrimPass = loadString;

  if (statusCode == ERROR_SUCCESS)
  {
    INT iVal;
    GetDlgItemText(hDlg, IDC_TXT_SETTINGS_TRIMMIN, loadString, MaxLoadString);
    if (swscanf_s(loadString, L"%d", &iVal) != 1)
      statusCode = IDS_ERROR_SETTINGS_TRIMMIN;
    
    if (statusCode == ERROR_SUCCESS)
      settings.TrimMin = iVal;
  }

  if (statusCode != ERROR_SUCCESS)
  {
    CString message;
    message.LoadStringW(statusCode);
    MessageBox(hDlg, message, L"Settings", MB_OK);
  }
  else
  {
    StickyTweetsStatus status = StickyTweetsAppXP::GetInstance().SaveSettings(settings);
    if (status == StatusStOk)
      status = redraw ? StatusStSettingsSaveRedraw : StatusStSettingsSave;
    
    EndDialog(hDlg, status);
  }
  return TRUE;
}